<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class TypeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return 后台板块管理首页
     */
    
    //后台板块管理首页
    public function getIndex(Request $request)
    {
        //每页显示
        $num = $request->input('num',8);
        //判断是否有关键词
        if($request->input('keywords')){

            $cates = DB::table('type')->where('name','like','%'.$request->input('keywords').'%')->orderBy('path')->paginate($num);

        }else{

            //查询数据
            $cates = DB::table('type')->orderBy('path')->paginate($num);
            
        }

        $data = $request->except('page');
        // dd($data);
        return view('Admin.type.index',['cates'=>$cates,'data'=>$data]);
    }

    //栏目添加
    public function getAdd()
    {
        //查询数据库的分类
        $cates = DB::table('type')->get();
        //返回添加post添加表单
        return view('Admin.type.add',['cates'=>$cates]);
    }

    //栏目添加操作
    public function postInsert(Request $request)
    {
       
        //接受数据
        $data = $request->except('_token');

        //判断pid是否为0
        if($data['pid'] == 0){

            $data['path']=0;
            $data['status']=1;
            $data['pname']=$request->input('name');

        }else{

            $res = DB::table('type')->where('id',$data['pid'])->first();
            $data['path']=$res->path.'-'.$data['pid'];
            $data['status']=1;
            $data['pname']=$res->name;

        }

        //入库操作
        $res = DB::table('type')->insertGetId($data);
        
        if($res){
            return redirect('/admin/type/index')->with('success','添加成功');
        }else{

            return back()->with('error','添加失败');
        }


    }

    //栏目删除操作
    public function postDel(Request $request)
    {
        
        //数据库查询是否有子类
        $num = DB::table('type')->where('pid','=',$request->id)->get();

        if($num != null){
            echo 0;
        }else{
            // $num = 0;

            $res = DB::table('type')->where('id','=',$request->id)->delete();
             echo $res;
        }
        
    }
   
   //修改栏目信息表单
    public function getEdit($id)
    {
        // dd($id);
        //数据库查询操作
        $res = DB::table('type')->where('id',$id)->first();

        return view('/admin/type/edit',['res'=>$res]);
    }

    //修改栏目信息操作
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        //取数据
        $data = $request->except('_token');
        //数据库操作
        $num = DB::table('type')->where('id',$request->id)->update($data);

        if($num){

            return redirect('/admin/type/index')->with('success','修改成功!');
        }else{
            return back()->with('error','修改失败');
        }
    }

    //修改显示状态
    public function getChange($id)
    {
        //查询状态
        $res = DB::table('type')->where('id',$id)->first();

        if($res->status == 1){

            $data['id'] = $id;
            $data['status'] = 0;
            $num = DB::table('type')->where('id',$id)->update($data);
        }else{
             $data['id'] = $id;
            $data['status'] = 1;
            $num = DB::table('type')->where('id',$id)->update($data);
        }

        if($num){

            return redirect('/admin/type/index')->with('success','状态修改成功');
        }else{
            return back()->with('error','状态修改失败');
        }
    }

}
